package cn.oms.file.provider.service.impl;


import cn.oms.file.api.service.IBarcodeService;
import cn.oms.file.provider.common.utils.DateUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;

/**
 * 条形码服务
 */

@RestController
@Service
@Slf4j
public class BarcodeServiceImpl implements IBarcodeService {


    @Value("${oms.file.image.barcode.upload.path}")
    public String fileUploadPath;

    @Value("${oms.file.resource.image.barcode.host}")
    public String resourceHost;

    /**
     * 生成条形码图片
     * 返回前端查看路径
     */
    @Override
    public String generateBarcodeImage(String text, String format, int width, int height, String fileName) {

        /**
         * 获取当前时间
         */
        //  按日期创建文件名称
        // path: 2022/03/24 17:28:30
        String datePath = new SimpleDateFormat(DateUtil.CN_DATE_FORMAT).format(new Date());

        // 文件名日期 20220324172830
        String date= datePath.replace("/", "").replace(":","").replace(" ","");

        //日期文件路径 2022/03/24
        String dateFilePath = DateTimeFormatter.ofPattern(DateUtil.CN_DATE_FORMAT_NO_DELIMITER).format(LocalDate.now());

        // 生成文件夹
        // D:/0.omsFile/image/Barcode/ + 2022/03/24
        File folder=new File(fileUploadPath,dateFilePath);
        folder.mkdirs();

        // 文件名 图片类型 日期 文件名 文件类型
        // barcode_20220324172830_hospital.png
        String fileName1 = "barcode" + "_" + date + "_" + fileName + "." + format;

        // 拼接前端访问的路径
        String resourcePath = resourceHost + "/" + dateFilePath + "/" + fileName1;

        // 文件路径拼接
        // D:/omsFile/image/QECode/文件名.文件格式
        String PATH = fileUploadPath  + "/" + dateFilePath + "/" + fileName1;

        // 生成二维码图片输出流
        Code128Writer code128Writer = new Code128Writer();
        //存储乱码问题
        HashMap map=new HashMap();
        map.put(EncodeHintType.CHARACTER_SET,"UTF-8");

        BitMatrix bitMatrix = null;
        try {

            //生成二维码信息对象
            bitMatrix = code128Writer.encode(text, BarcodeFormat.CODE_128, width, height,map);
        } catch (WriterException e) {
            e.printStackTrace();
        }

        try {
            //将二维码写入指定路径的文件中
            MatrixToImageWriter.writeToPath(bitMatrix, format, Paths.get(PATH));
            log.info("二维码生成成功！！！文件地址：{}",PATH);
        } catch (IOException e) {
            e.printStackTrace();
        }

        log.info("前端访问路径地址：{}",resourcePath);
        // 返回资源访问路径
        return resourcePath;

    }

}
